﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace ZK.MyControl
{
   public  class DataViewTable<T>: DataGridView where T : new()
    {
       
       public DataViewTable( ) 
       {

            #region DataGridVeiw Style
            this.Dock = DockStyle.Fill;
            this.Font = new System.Drawing.Font("宋体", 15); 
            System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
           System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle();
           this.AllowUserToAddRows = false;
           this.AllowUserToDeleteRows = false;
            this.AutoGenerateColumns = false;
           dataGridViewCellStyle1.BackColor = System.Drawing.Color.FromArgb(231, 232, 239);
           this.AlternatingRowsDefaultCellStyle = dataGridViewCellStyle1;
           this.BackgroundColor = System.Drawing.Color.White;
           this.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
           this.ColumnHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.Single;
           dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;//211, 223, 240
           //dataGridViewCellStyle2.BackColor = System.Drawing.Color.FromArgb(223, 235, 248);
           dataGridViewCellStyle2.BackColor = System.Drawing.Color.FromArgb(0xE7, 0xE8, 0xEF);
           //dataGridViewCellStyle2.Font = new System.Drawing.Font("宋体", 14);//列头字体
           dataGridViewCellStyle2.ForeColor = System.Drawing.Color.Black;
           dataGridViewCellStyle2.SelectionBackColor = System.Drawing.SystemColors.Highlight;
           dataGridViewCellStyle2.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
           dataGridViewCellStyle2.Padding = new Padding(0,5,0,5);
         
           this.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle2;
           this.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
           this.EnableHeadersVisualStyles = false;
           this.GridColor = System.Drawing.SystemColors.ControlDark;
           this.ReadOnly = true;
           this.RowHeadersVisible = true;
           this.RowHeadersWidth = 70;
           this.RowTemplate.Height = 28;
           this.RowTemplate.ReadOnly = true;
           this.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
           this.MultiSelect = false;
           this.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
           this.AllowUserToResizeRows = false;
           this.AllowUserToResizeColumns = false;
           this.RowHeadersDefaultCellStyle.BackColor = System.Drawing.Color.FromArgb(231, 232, 239);
           this.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
            #endregion

            T t = new T();
            this.Columns.Clear();
            PropertyInfo[] propertys = t.GetType().GetProperties();
            foreach (PropertyInfo property in propertys)
            {

                string name = property.Name;
                object value = property.GetValue(t, null);
                DescriptionAttribute descriptionAttribute = (DescriptionAttribute)Attribute.GetCustomAttribute(property, typeof(DescriptionAttribute));

                if (descriptionAttribute != null)
                {
                    string des = descriptionAttribute.Description;//  
                    this.Columns.Add(name, des);
                    this.Columns[name].DataPropertyName = name;
                    if (property.PropertyType== typeof(int)|| property.PropertyType == typeof(decimal))
                    {
                        this.Columns[name].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                    }
                }
                else {
                    this.Columns.Add(name, name);
                    this.Columns[name].DataPropertyName = name;
                    this.Columns[name].Visible = false;
                }
            
            }

            this.RowPostPaint += DataViewTable_RowPostPaint;
        }

        private void DataViewTable_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            var grid = sender as DataGridView;
            var rowIdx = (e.RowIndex + 1).ToString();

            var centerFormat = new StringFormat()
            {
                // right alignment might actually make more sense for numbers  
                Alignment = StringAlignment.Center,
                LineAlignment = StringAlignment.Center
            };

            var headerBounds = new Rectangle(e.RowBounds.Left, e.RowBounds.Top, grid.RowHeadersWidth, e.RowBounds.Height);
            e.Graphics.DrawString(rowIdx, this.Font, SystemBrushes.ControlText, headerBounds, centerFormat);
        }

  
    }
}
